package com.block.common.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.constant.RedisKeyConstant;
import com.block.common.constant.SysConstant;
import com.block.common.service.ISysConstantInfoService;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.SysConstantInfo;
import com.block.db.mapper.SysConstantInfoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2022-08-20
 */
@Service
public class SysConstantInfoServiceImpl extends ServiceImpl<SysConstantInfoMapper, SysConstantInfo> implements ISysConstantInfoService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public ResponseVo getAppConfigInfo() {
        SysConstantInfo constantInfo = this.baseMapper.getEntryByType(SysConstant.TYPE_APP_CONFIG_INFO);
        JSONObject jsonObject = JSON.parseObject(constantInfo.getValue());
        return new ResponseVo(AppRspCodeConstant.SUCCESS, jsonObject);
    }

    @Override
    public ResponseVo getNoticeList() {
        SysConstantInfo constantInfo = this.baseMapper.getEntryByType(SysConstant.TYPE_NOTICE_LIST);
        JSONArray jsonArray = JSON.parseArray(constantInfo.getValue());

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("dataList", jsonArray);

        return new ResponseVo(AppRspCodeConstant.SUCCESS, jsonObject);
    }

    @Override
    public List<String> getAdminMobileList()
    {

        String redisKey = RedisKeyConstant.LIST_ADMIN_MOBILE;
        String result = stringRedisTemplate.opsForValue().get(redisKey);
        List<String> resultList = new ArrayList<>();
        if (StrUtil.isNotEmpty(result)){
             resultList = JSON.parseArray(result, String.class);
             if (CollUtil.isNotEmpty(resultList)){
                 return resultList;
             }
        }
        SysConstantInfo constantInfo = this.baseMapper.getEntryByType(SysConstant.TYPE_ADMIN_MOBILE_LIST);
        stringRedisTemplate.opsForValue().set(redisKey, constantInfo.getValue());
        resultList = JSON.parseArray(constantInfo.getValue(), String.class);

        return resultList;
    }

    @Override
    public List<String> getNotPushChannelInfo() {
        String redisKey = RedisKeyConstant.LIST_NOT_PUSH_CHANNEL;
        String result = stringRedisTemplate.opsForValue().get(redisKey);
        List<String> resultList = new ArrayList<>();
        if (StrUtil.isNotEmpty(result)){
            resultList = JSON.parseArray(result, String.class);
            if (CollUtil.isNotEmpty(resultList)){
                return resultList;
            }
        }
        SysConstantInfo constantInfo = this.baseMapper.getEntryByType(SysConstant.TYPE_NOT_PUSH_CHANNEL_LIST);
        stringRedisTemplate.opsForValue().set(redisKey, constantInfo.getValue());
        resultList = JSON.parseArray(constantInfo.getValue(), String.class);

        return resultList;
    }
}
